﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;


namespace IntroduceCompany.Util
{
    public static class MyAntiForgeryExtensions
    {
        // Methods
        public static string MyAntiForgeryToken(this HtmlHelper helper)
        {
            return MyAntiForgeryToken(helper, null);
        }

        public static string MyAntiForgeryToken(this HtmlHelper helper, string salt)
        {
            string fragment;
            string path = helper.ViewContext.HttpContext.Request.ApplicationPath;
            try
            {
                fragment = helper.AntiForgeryToken(salt, null, path).ToHtmlString();
            }
            catch (HttpAntiForgeryException)
            {
                // okay, scrub the cookie and have another go.
                string cookieName = GetAntiForgeryTokenName(path);
                helper.ViewContext.HttpContext.Request.Cookies.Remove(cookieName);
                fragment = helper.AntiForgeryToken(salt, null, path).ToHtmlString();
            }
            return fragment;
        }

        #region AntiForgeryData code that shouldn't be sealed
        // Copied from AntiForgeryData since they aren't accessible.
        internal static string GetAntiForgeryTokenName(string appPath)
        {
            if (String.IsNullOrEmpty(appPath))
            {
                return "__RequestVerificationToken";
            }
            else
            {
                return "__RequestVerificationToken_" + Base64EncodeForCookieName(appPath);
            }
        }
        private static string Base64EncodeForCookieName(string s)
        {
            byte[] rawBytes = Encoding.UTF8.GetBytes(s);
            string base64String = Convert.ToBase64String(rawBytes);

            // replace base64-specific characters with characters that are safe for a cookie name
            return base64String.Replace('+', '.').Replace('/', '-').Replace('=', '_');
        }
        #endregion
    }
}